using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    class clsIMPA_CODE
    {
        public clsIMPA_CODE(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }
        public List<IMPA_CODE> DataTableToLstObj(DataTable dt)
        {
            IMPA_CODE impaCodeDTO;
            List<IMPA_CODE> lstImpaCodeDTO = new List<IMPA_CODE>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                impaCodeDTO = new IMPA_CODE();
                try
                {
                    impaCodeDTO._code = dt.Rows[i]["Code"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    impaCodeDTO._desc1 = dt.Rows[i]["Desc1"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    impaCodeDTO._desc2 = dt.Rows[i]["Desc2"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    impaCodeDTO._unit = dt.Rows[i]["Unit"].ToString();
                }
                catch (Exception ex)
                {
                }
                lstImpaCodeDTO.Add(impaCodeDTO);
            }
            return lstImpaCodeDTO;
        }
        public IMPA_CODE DataTableToObj(DataTable dt)
        {
            IMPA_CODE impaCodeDTO;
            impaCodeDTO = new IMPA_CODE();

            try
            {
                impaCodeDTO._code = dt.Rows[0]["Code"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                impaCodeDTO._desc1 = dt.Rows[0]["Desc1"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                impaCodeDTO._desc2 = dt.Rows[0]["Desc2"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                impaCodeDTO._unit = dt.Rows[0]["Unit"].ToString();
            }
            catch (Exception ex)
            {
            }

            return impaCodeDTO;
        }
        public List<IMPA_CODE> LoadImpaCodeByL2(int l2)
        {
            DataTable dt = new DataTable();
            List<IMPA_CODE> lstImpaCodeDTO = new List<IMPA_CODE>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectIMPA_CODE_ByL2";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@l2", l2);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstImpaCodeDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstImpaCodeDTO;
        }
        public List<IMPA_CODE> LoadImpaCodeByL2AndL3(int l2, int l3)
        {
            DataTable dt = new DataTable();
            List<IMPA_CODE> lstImpaCodeDTO = new List<IMPA_CODE>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectIMPA_CODE_ByL2&L3";
                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@l2", l2);
                par[1] = new SqlParameter("@l3", l3);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstImpaCodeDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstImpaCodeDTO;
        }
        public IMPA_CODE LoadImpaCodeByCode(string code)
        {
            DataTable dt = new DataTable();
            IMPA_CODE ImpaCodeDTO = new IMPA_CODE();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectIMPA_CODE";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@Code", code);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                ImpaCodeDTO = DataTableToObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return ImpaCodeDTO;
        }
        public List<IMPA_CODE> LoadImpaTreeCodeByJpgname(string jpgname)
        {
            DataTable dt = new DataTable();
            List<IMPA_CODE> lstImpaCodeDTO = new List<IMPA_CODE>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectIMPA_TREE_CODE_ByJpgname";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@Jpgname", jpgname);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstImpaCodeDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstImpaCodeDTO;
        }
        public List<IMPA_CODE> LoadImpaCodeByCodeAndKey(string code, string key, string maKho)
        {
            DataTable dt = new DataTable();
            List<IMPA_CODE> lstImpaCodeDTO = new List<IMPA_CODE>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectIMPA_CODE_ByCodeAndKey";
                SqlParameter[] par = new SqlParameter[3];
                par[0] = new SqlParameter("@code", code == "" ? null : code);
                par[1] = new SqlParameter("@key", key == "" ? null : key);
                par[2] = new SqlParameter("@maKho", maKho == "" ? null : maKho);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstImpaCodeDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstImpaCodeDTO;
        }
        public List<VAT_TU_PHU_TUNG> ConvertImpaToVTPT(List<IMPA_CODE> lstICode)
        {
            List<VAT_TU_PHU_TUNG> LstVtpt = new List<VAT_TU_PHU_TUNG>();
            VAT_TU_PHU_TUNG vtptDTO;
            foreach (IMPA_CODE iCode in lstICode)
            {
                vtptDTO = new VAT_TU_PHU_TUNG();
                vtptDTO._maVTPT = iCode._code;
                vtptDTO._tenVTPT = iCode._desc1 + " " + iCode._desc2;
                vtptDTO._donViTinh = iCode._unit;
                LstVtpt.Add(vtptDTO);
            }
            return LstVtpt;
        }
    }
}
